package com.tedu.weibo.controller;

import com.tedu.weibo.mapper.WeiboMapper;
import com.tedu.weibo.pojo.dto.WeiboDTO;
import com.tedu.weibo.pojo.entity.User;
import com.tedu.weibo.pojo.entity.Weibo;
import com.tedu.weibo.pojo.vo.WeiboDetailVO;
import com.tedu.weibo.pojo.vo.WeiboVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "微博模块")
@RestController
@RequestMapping("/v1/weibo")
@Slf4j
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    @ApiOperation("发表微博模块")
    @PostMapping("/insert")
    public int insert(@RequestBody WeiboDTO weiboDTO, HttpSession session){
        log.info("WeiboController.insert()发表微博");
        log.trace("开始执行insert方法");//记录程序运行轨迹
        log.debug("weiboDTO:"+weiboDTO);//记录程序处理结果,根据它辅助排查逻辑是否有误
        User user = (User) session.getAttribute("user");
        log.trace("通过session获取当前用户信息");//记录程序运行轨迹
        log.debug("user:"+user);//记录程序处理结果
        if (user == null) {
            log.trace("user为null用户未登录");
            return 2;
        }else{
            log.trace("user不为null,处理发表微博");
            Weibo weibo = new Weibo();
            BeanUtils.copyProperties(weiboDTO, weibo);
            weibo.setCreated(new Date());
            weibo.setUserId(user.getId());
            weiboMapper.insert(weibo);
            return 1;
        }
    }
    @ApiOperation("获取首页微博列表模块")
    @GetMapping("/selectIndex")
    public List<WeiboVO> selectIndexWeibo(){
        return weiboMapper.selectWeibo();
    }

    @ApiOperation("获取微博详情模块")
    @GetMapping("/selectById")
    public WeiboDetailVO selectWeiboDetail(int id){
        return weiboMapper.selectWeiboById(id);
    }

}
